import $ from 'jquery'
import eleData from './RichElement.js'
import eleOper from './ElementOperator.js'
import pasteRevole from '../plugins/PasteRevole.js'
import chinesWord from './ChineseWord.js'

const keyEvenDeal = {
	backspace (id, e) {
		let children = $(id).children();
		let node = eleOper.getSelectNode();
		if ($(node).attr("tag") && $(node).attr("tag").toLowerCase() == eleData.header.id && (node.childNodes.length == 1 && $(node).text().length == 0)) {
			e.stopPropagation();
			e.preventDefault();
			return;
		}
		if (children.length == 1 && children[0].childNodes.length==1 && $(children[0]).text().length == 0) {
			e.stopPropagation();
			e.preventDefault();
			return;
		}
		if ($(node).attr("tag") == eleData.image.id) {
			let preNode = $(node).prev();
			node.remove();
			eleOper.updateRange(preNode.get(0));
			e.stopPropagation();
			e.preventDefault();
			return;
		}
		if ($(node).attr("tag") == eleData.code.id) {
			// 这里处理数据信息
			let cld = $(node).children();
			let preNode = $(node).prev();
			if (cld.length <= 1) {
				$(node).remove();
				eleOper.updateRange(preNode.get(0));
				e.stopPropagation();
				e.preventDefault();
				return;
			}
			let codeNodes = [];
			for(let i = 0; i < cld.length; i++) {
				if ($(cld[i]).attr('tag') == 'body') {
					codeNodes = $(cld[i]).children();
					break;
				}
			}
			keyEvenDeal.dealCodeBody(codeNodes, node);
			return;
		}
		if ($(node).attr("tag") == 'title') {
			let temp = [];
			temp.push(node);
			keyEvenDeal.dealCodeBody(temp, $(node).parent());
			return;
		}
		if ($(node).attr("tag") == 'body') {
			keyEvenDeal.dealCodeBody($(node).children(), $(node).parent());
			return;
		}
		if ($(node).attr("tag") == eleData.code.childId) {
			keyEvenDeal.dealCodeBody($(node).parent().children(), $(node).parent().parent(), e);
			return;
		}
		chinesWord.backspace(e, node);
	},
	dealCodeBody(codeNodes, node, e) {
		let select = window.getSelection().getRangeAt(0);
		let n = eleOper.getSelectNode();
		if (select.startOffset == 0 && $(n).attr('tag') == eleData.code.childId) {
			let prev = $(n).prev();
			if ((prev == null || prev == undefined || prev.length == 0) && codeNodes.length > 1) {
				e.stopPropagation();
				e.preventDefault();
				return;
			}
			if (codeNodes.length == 1 && $(codeNodes[0]).text().length == 0) {
				let preNode = $(node).prev();
				$(node).remove();
				eleOper.updateRange(preNode.get(0));
				e.stopPropagation();
				e.preventDefault();
				return;
			}
			$(prev).text($(prev).text() + $(n).text());
			$(n).remove();
			eleOper.updateRange(prev.get(0));
			return;
		}
		if ((codeNodes.length == 1 && $(codeNodes[0]).text().length == 0)) {
			let preNode = $(node).prev();
			$(node).remove();
			eleOper.updateRange(preNode.get(0));
			e.stopPropagation();
			e.preventDefault();
		}
		if ((codeNodes.length == 1 && $(codeNodes[0]).text().length == 1)) {
			$(codeNodes[0]).text('');
			e.stopPropagation();
			e.preventDefault();
		}
	},
	enter (id, e) {
		let children = $(id).children();
		let n = eleOper.getSelectNode();
		// console.log(n);
		if (children.length <= 1) {
			let el = eleOper.createElement($(id), eleData.content);
			let range = document.createRange();
			range.selectNodeContents(el);
			range.collapse(false);
			let sel = window.getSelection();
			sel.removeAllRanges();
			sel.addRange(range);
			e.stopPropagation();
			e.preventDefault();
			return;
		} else if ($(n).attr("tag") == 'image') {
			eleOper.insertCommonNode(eleData.content);
			e.stopPropagation();
			e.preventDefault();
			return;
		} else if ($(n).attr("tag") == eleData.header.id) {
			eleOper.insertCommonNode(eleData.content);
			e.stopPropagation();
			e.preventDefault();
			return;
		} else if ($(n).attr("tag") == 'codeChildren' || $(n).attr("tag") == eleData.code.id || $(n).attr("tag") == 'body') {
			if ($(n).text().length > 0) {
				// 这里表示创建代码行的信息
				keyEvenDeal.dealCodeContents(n, e);
				// eleOper.createCodeItem(n);
				// e.stopPropagation();
				// e.preventDefault();
				return;
			}
			let next = $(n).next();
			if (next == null || next == undefined || next == '') {
				eleOper.createCodeToContent(n);
				e.stopPropagation();
				e.preventDefault();
				return;
			}
			if (next.attr("tag") == "codeChildren" || next.attr("tag") == eleData.code.id) {
				// eleOper.createCodeItem(n);
				// e.stopPropagation();
				// e.preventDefault();
				keyEvenDeal.dealCodeContents(n, e);
				return;
			}
			eleOper.createCodeToContent(n);
			e.stopPropagation();
			e.preventDefault();
			return;
			
		} else if ($(n).attr("tag") == 'listChildren') {
			let text = $(n).text();
			if (text.length > 0) {
				return;
			}
			let net = $(n).next();
			if (net.length != 0) {
				e.stopPropagation();
				e.preventDefault();
				return;
			}
			eleOper.insertCommonNode(eleData.content, true, 2);
			$(n).remove();
			e.stopPropagation();
			e.preventDefault();
			return;
		} else if ($(n).attr('tag') == eleData.title1.id || $(n).attr('tag') == eleData.title2.id) {
			eleOper.insertCommonNode(eleData.content);
			e.stopPropagation();
			e.preventDefault();
			return;
		}
		chinesWord.enter(e, n);
	},
	dealCodeContents(n, e) {
		let select = window.getSelection().getRangeAt(0);
		let content = '';
		let indent = 0;
		if ($(n).attr("tag") == 'codeChildren') {
			let offset = select.startOffset;
			let text = $(n).text();
			let realText = text.trimStart();
			let seat = offset + text.length - realText.length;
			let prefix = text.substring(0, seat);
			content = text.substring(seat);
			let idt = $(n).attr("indent");
			if (idt != null && idt != undefined && idt != '') {
				indent = parseInt(idt);
			}
			$(n).text(prefix);
			$(n).append($("<br/>"))
		}
		let node = eleOper.createCodeItem(n, content, false);
		$(node).css({"text-indent": indent + "em"});
		$(node).attr("indent", indent);
		e.stopPropagation();
		e.preventDefault();
		return;
	},
	tab (e) {
		let select = window.getSelection().getRangeAt(0);
		let tagEle = eleOper.getElements(select);
		for (let i = 0; i < tagEle.length; i ++) {
			let tag = $(tagEle[i]).attr("tag");
			if (tag == eleData.chineseWord.contentId || tag == eleData.chineseWord.pingyinId || tag == eleData.chineseWord.id) {
				continue;
			}
			if (tag == eleData.code.id) {
				let children = $(tagEle[i]).children();
				for(let j = 0; j < children.length; j++) {
					if ($(children[j]).attr('tag') == 'body') {
						keyEvenDeal.tabCode(children[j]);
						break;
					}
				}
				continue;
			}
			if (tag == 'body') {
				keyEvenDeal.tabCode(tagEle[i]);
				continue;
			}
			if (tag == 'title') continue;
			if (tag == eleData.image.id) continue;
			// $(tagEle[i]).prepend("&nbsp;&nbsp;")
			let indent = $(tagEle[i]).attr("indent");
			if (indent == null || indent == undefined || indent == '') {
				indent = 0;
			} else {
				indent = parseInt(indent);
			}
			indent = indent + 2;
			$(tagEle[i]).css({"text-indent": indent + "em"});
			$(tagEle[i]).attr("indent", indent);
		}
		e.stopPropagation();
		e.preventDefault();
	},
	tabCode(node) {
		let children = $(node).children();
		for(let i = 0; i < children.length; i++) {
			let indent = $(children[i]).attr("indent");
			if (indent == null || indent == undefined || indent == '') {
				indent = 0;
			} else {
				indent = parseInt(indent);
			}
			indent += 2;
			$(children[i]).css({"text-indent": indent + "em"});
			$(children[i]).attr("indent", indent);
		}
	},
	altCode(node) {
		let children = $(node).children();
		for(let i = 0; i < children.length; i++) {
			let indent = $(children[i]).attr("indent");
			if (indent == null || indent == undefined || indent == '') {
				indent = 0;
			} else {
				indent = parseInt(indent);
			}
			if (indent <= 0) {
				continue;
			}
			indent -= 2;
			$(children[i]).css({"text-indent": indent + "em"});
			$(children[i]).attr("indent", indent);
		}
	},
	alt (e) {
		let select = window.getSelection().getRangeAt(0);
		let tagEle = eleOper.getElements(select);
		for (let i = 0; i < tagEle.length; i ++) {
			let tag = $(tagEle[i]).attr("tag");
			if (tag == eleData.chineseWord.contentId || tag == eleData.chineseWord.pingyinId || tag == eleData.chineseWord.id) {
				continue;
			}
			if (tag == eleData.code.id) {
				let children = $(tagEle[i]).children();
				for(let j = 0; j < children.length; j++) {
					if ($(children[j]).attr('tag') == 'body') {
						keyEvenDeal.altCode(children[j]);
						break;
					}
				}
				continue;
			}
			if (tag == 'body') {
				keyEvenDeal.altCode(tagEle[i]);
				continue;
			}
			if (tag == 'title') continue;
			if (tag == eleData.image.id) continue;
			let indent = $(tagEle[i]).attr("indent");
			if (indent == null || indent == undefined || indent == '') {
				indent = 0;
			} else {
				indent = parseInt(indent);
			}
			if (indent <= 0) {
				continue;
			}
			indent = indent - 2;
			$(tagEle[i]).css({"text-indent": indent + "em"});
			$(tagEle[i]).attr("indent", indent);
			// if ($(tagEle[i]).attr("tag") == eleData.image.id) continue;
			// let text = $(tagEle[i]).html();
			// if (text.indexOf('&nbsp;&nbsp;') == 0) {
			// 	text = text.substring('&nbsp;&nbsp;'.length, text.length);
			// 	$(tagEle[i]).html(text);
			// } else if (text.indexOf('&nbsp;') == 0) {
			// 	text = text.substring('&nbsp;'.length, text.length);
			// 	$(tagEle[i]).html(text);
			// }
		}
		e.stopPropagation();
		e.preventDefault();
	},
	ctrlV (event, type = 'common') {
		//上传用户粘贴的图片
		var isChrome = false
		if (event.clipboardData || event.originalEvent) {
			//not for ie11  某些chrome版本使用的是event.originalEvent
			var clipboardData =
				event.clipboardData || event.originalEvent.clipboardData
			if (clipboardData.items) {
				isChrome = true
				pasteRevole.main(clipboardData, event, type);
			}
		}
	}
}

export default keyEvenDeal;